package com.wk.code.wkserver.controller.app;

import com.wk.code.wkserver.controller.common.BaseController;
import com.wk.code.wkserver.request.app.DemandRequest;
import com.wk.code.wkserver.request.app.SourceCodeRequest;
import com.wk.code.wkserver.request.base.BasePageRequest;
import com.wk.code.wkserver.response.admin.SourceCodeResponse;
import com.wk.code.wkserver.response.app.AppDemandResponse;
import com.wk.code.wkserver.response.app.AppSourceCodeResponse;
import com.wk.code.wkserver.response.app.SourceCodeDetailResponse;
import com.wk.code.wkserver.response.app.SourceCodeListResponse;
import com.wk.code.wkserver.response.result.R;
import com.wk.code.wkserver.service.ISourceCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wq
 * @since 2021-08-15
 */
@RestController
@RequestMapping("/sourceCode")
@AllArgsConstructor
@Api(tags = "app-前台源码管理")
public class SourceCodeController extends BaseController {

    final ISourceCodeService sourceCodeService;


    /**
     * 发布系统源码
     *
     * @param request 请求参数
     * @return R
     */
    @ApiOperation(value = "发布系统源码", notes = "发布系统源码")
    @PostMapping("/publish")
    public R<Void> publish(@RequestBody SourceCodeRequest request) {
        sourceCodeService.publish(getUserId(), request);
        return R.success();
    }


    /**
     * 我的源码列表
     *
     * @return R
     */
    @ApiOperation(value = "我的源码列表")
    @GetMapping("/mineCode")
    public R<List<AppSourceCodeResponse>> mineDemandList() {
        return R.success(sourceCodeService.mineSourceCode(getUserId()));
    }


    /**
     * 更新浏览量
     *
     * @param id 需求ID
     * @return R
     */
    @ApiOperation(value = "更新浏览量")
    @GetMapping("/updateViewCount")
    public R<Void> updateViewCount(Integer id) {
        sourceCodeService.updateViewCount(id);
        return R.success();
    }


    /**
     * 码市列表
     *
     * @param request 请求参数
     * @return R
     */
    @ApiOperation(value = "码市列表")
    @PostMapping("/marketList")
    public R<List<SourceCodeListResponse>> marketList(@RequestBody BasePageRequest request) {
        return R.success(sourceCodeService.marketList(request));
    }

    /**
     * 详情
     *
     * @param id ID
     * @return R
     */
    @ApiOperation(value = "详情")
    @GetMapping("/detail")
    public R<SourceCodeDetailResponse> detail(Integer id) {
        return R.success(sourceCodeService.detail(id));
    }


    /**
     * 删除源码
     *
     * @param sourceCodeId 源码ID
     * @return R<Void>
     */
    @ApiOperation(value = "删除源码")
    @GetMapping("/deleteSourceCode")
    public R<Void> deleteDemand(Integer sourceCodeId) {
        sourceCodeService.deleteSourceCode(sourceCodeId, getUserId());
        return R.success();
    }

}
